#delimit ;  
clear;
capture log close;
set more 1;  
local UNIX 2;
* set this to 1 if include 2020 wave;
global INCLUDE2020 1;

if `UNIX' == 1 {;
                 local saved  "C:\Dropbox\hrs\merge\";
                 local trackd "C:\Dropbox\hrs\tracker";
                 local wave1d "C:\Dropbox\hrs\merge";
                 local wave2d "C:\Dropbox\hrs\merge";
                 local wave3d "C:\Dropbox\hrs\merge";
                 local wave4d "C:\Dropbox\hrs\merge";
                 local wave5d "C:\Dropbox\hrs\merge";
		         local wave6d "C:\Dropbox\hrs\merge";
		         local wave7d "C:\Dropbox\hrs\merge";
		         local wave8d "C:\Dropbox\hrs\merge";
                 local aheadw1 "~/hrs/aheadw1/";
                 local aheadw2 "~/hrs/aheadw2/"; };
else {;
   *global folder "\\econ-san-fs\Home4\uctpkp0\SystemFolder\Windows7\Desktop";
    global folder "D:\Dropbox";
                 local saved   "$folder\hrs\merge\";
                 local trackd  "$folder\hrs\tracker\";
                 local wave1d  "$folder\hrs\wave1\";
                 local wave2d  "$folder\hrs\wave2\";
                 local wave3d  "$folder\hrs\wave3\";
                 local wave4d  "$folder\hrs\wave4\";
                 local wave5d  "$folder\hrs\wave5\";
		         local wave6d  "$folder\hrs\wave6\";
                 local wave7d  "$folder\hrs\wave7\";
                 local wave8d  "$folder\hrs\wave8\";
				 local wave9d  "$folder\hrs\wave9\";
				 local wave10d "$folder\hrs\wave10\";
				 local wave11d "$folder\hrs\wave11\";
				 local wave12d "$folder\hrs\wave12\";	
				 local wave13d "$folder\hrs\wave13\";
				 local wave14d "$folder\hrs\wave14\";
				 local wave15d "$folder\hrs\wave15\";
                 local aheadw1 "$folder\hrs\aheadw1\";
                 local aheadw2 "$folder\hrs\aheadw2\";};


set mem 300m;

log using `saved'retire.log, replace; 




*Create workpref;
*-------- wave 13 --------;
use `wave13d'H16J_R;
keep HHID PN PJ552;
replace PJ552=. if PJ552>4;
gen workpref13= PJ552;
drop PJ552;
sort HHID PN;
sleep 1000; save `saved'retplans, replace;
*-------- wave 14 --------;
use `wave14d'H18J_R;
rename hhid HHID;
rename pn PN;
keep HHID PN QJ552;
replace QJ552=. if QJ552>4;
gen workpref14= QJ552;
drop QJ552;
sort HHID PN;
merge HHID PN using `saved'retplans;
drop _merge;
sort HHID PN;
sleep 1000; save `saved'retplans, replace;
if $INCLUDE2020 {; 
*-------- wave 15 --------;
use `wave15d'H20J_R;
rename hhid HHID;
rename pn PN;
keep HHID PN RJ552;
replace RJ552=. if RJ552>4;
gen workpref12= RJ552;
drop RJ552;
sort HHID PN;
merge HHID PN using `saved'retplans;
drop _merge;
sort HHID PN;
sleep 1000; save `saved'retplans, replace;
};
*-------- wave 12 --------;
use `wave12d'H14J_R;
keep HHID PN OJ552;
replace OJ552=. if OJ552>4;
gen workpref12= OJ552;
drop OJ552;
sort HHID PN;
merge HHID PN using `saved'retplans;
drop _merge;
sort HHID PN;
sleep 1000; save `saved'retplans, replace;
*-------- wave 11 --------;
use  `wave11d'H12J_R;
keep HHID PN NJ552;
replace NJ552=. if NJ552>4;
gen workpref11= NJ552;
drop NJ552;
sort HHID PN;
merge HHID PN using `saved'retplans;
drop _merge;
sort HHID PN;
sleep 1000; save `saved'retplans, replace;
*-------- wave 10 --------;
use `wave10d'H10J_R;
keep HHID PN MJ552;
replace MJ552=. if MJ552>4;
gen workpref10= MJ552;
drop MJ552;
sort HHID PN;
merge HHID PN using `saved'retplans;
drop _merge;
sort HHID PN;
sleep 1000; save `saved'retplans, replace;
*-------- wave 9 --------;
use `wave9d'H08J_R;
keep HHID PN LJ552;
replace LJ552=. if LJ552>4;
gen workpref9= LJ552;
drop LJ552;
sort HHID PN;
merge HHID PN using `saved'retplans;
drop _merge;
sort HHID PN;
sleep 1000; save `saved'retplans, replace;
*-------- wave 8 --------;
use `wave8d'H06J_R;
keep HHID PN KJ552;
replace KJ552=. if KJ552>4;
gen workpref8= KJ552;
drop KJ552;
sort HHID PN;
merge HHID PN using `saved'retplans;
drop _merge;
sort HHID PN;
sleep 1000; save `saved'retplans, replace;
*-------- wave 7 --------;
use `wave7d'H04J_R;
keep HHID PN JJ552;
replace JJ552=. if JJ552>4;
gen workpref7= JJ552;
drop JJ552;
sort HHID PN;
merge HHID PN using `saved'retplans;
drop _merge;
sort HHID PN;
sleep 1000; save `saved'retplans, replace;
*-------- wave 6 --------;
use `wave6d'H02J_R;
keep HHID PN HJ552;
replace HJ552=. if HJ552>4;
gen workpref6= HJ552;
drop HJ552;
sort HHID PN;
merge HHID PN using `saved'retplans;
drop _merge;
sort HHID PN;
sleep 1000; save `saved'retplans, replace;
*-------- wave 5 --------;
use `wave5d'H00G_R;
rename G3835 Q3835;
keep HHID PN Q3835;
replace Q3835=. if Q3835>4;
gen workpref5= Q3835;
drop Q3835;
sort HHID PN;
merge HHID PN using `saved'retplans;
drop _merge;
sort HHID PN;
sleep 1000; save `saved'retplans, replace;
*-------- wave 4 --------;
use `wave4d'H98G_R;
rename F3546 Q3546;
keep HHID PN Q3546;
replace Q3546=. if Q3546>4;
gen workpref4= Q3546;
drop Q3546;
sort HHID PN;
merge HHID PN using `saved'retplans;
drop _merge;
sort HHID PN;
sleep 1000; save `saved'retplans, replace;
*-------- wave 3 --------;
use `wave3d'H96G_R;
rename E3017 Q3017;
keep HHID PN Q3017;
replace Q3017=. if Q3017>4;
gen workpref3= Q3017;
drop Q3017;
sort HHID PN;
merge HHID PN using `saved'retplans;
drop _merge;
sort HHID PN;
sleep 1000; save `saved'retplans, replace;
*-------- wave 2 --------;                                                      //workpref2=0 exists (not in other waves) - I think just setting it to missing is not correct;
use `wave2d'w2fa;
keep HHID PN W3955;
replace W3955=. if W3955>4|W3955==0;	
gen workpref2 = W3955;
drop W3955;
sort HHID PN;
merge HHID PN using `saved'retplans;
drop _merge;
sort HHID PN;
sleep 1000; save `saved'retplans, replace;

*self employed;
use `wave2d'w2fb;
keep HHID PN W4610;
replace W4610=. if W4610>4;
sort HHID PN;
merge HHID PN using `saved'retplans;
drop _merge;
replace workpref2 = W4610 if (workpref2~=1|workpref2~=2|workpref2~=3|workpref2~=4) & (W4610==1|W4610==2|W4610==3|W4610==4); //there is nobody with W3955 in 1-4 and W4610 in 1-4;
drop W4610;
sort HHID PN;
sleep 1000; save `saved'retplans, replace;
*-------- wave 1 --------;
use `saved'retplans;
gen workpref1=.;
sort HHID PN;
sleep 1000; save `saved'retplans, replace;
*--------------------------------------;




use `wave1d'employer.dta;

keep HHID PN V5001 V5009 V5011 V5012 V5013 V5014 V5015 V5016 V5017 V5018 V5019 V5031 V3319;
* note: if i want measures of the disutility of leisure for non-workers, see V4391-V4393;

replace V5009 = . if V5009 == 8 | V5009 == 9; 
replace V5011 = . if V5011 == 8 | V5011 == 9; 
replace V5012 = . if V5012 == 8 | V5012 == 9; 
replace V5013 = . if V5013 == 8 | V5013 == 9; 
replace V5014 = . if V5014 == 8 | V5014 == 9; 
replace V5015 = . if V5015 == 8 | V5015 == 9; 
replace V5016 = . if V5016 == 8 | V5016 == 9; 
replace V5017 = . if V5017 == 8 | V5017 == 9; 
replace V5018 = . if V5018 == 8 | V5018 == 9; 
replace V5019 = . if V5019 == 8 | V5019 == 9; 
replace V5031 = . if V5031 == 8 | V5031 == 9; 
replace V3319 = . if V3319 == 8 | V3319 == 9; 

sort HHID PN;

merge HHID PN using `saved'retplans;
drop _merge;
sort HHID PN;

sleep 1000; save `saved'retplans.dta, replace;

clear;

use `wave1d'modulee.dta;

keep HHID PN V9063;

sort HHID PN;
merge HHID PN using `saved'retplans.dta;
drop _merge;
gen temp=real(HHID); 
drop HHID; 
rename temp HHID; 
gen temp=real(PN); 
drop PN; 
rename temp PN; 
sort HHID PN; 
sleep 1000; save `saved'retplans.dta, replace;




log close;

* compare summary stats before and after;
cd "$folder\hrs\merge\compare\" ;
summtab, contvars(_all) mean median range excel excelname(retplans.xlsx) replace;
cd "$folder\hrs\merge\" ;
use `saved'retplans.old.dta , clear;
cd "$folder\hrs\merge\compare\" ;
summtab, contvars(_all) mean median range excel excelname(retplans.xlsx) sheetname(Old);

clear;

